package com.lxs.legou.pay.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;

/**
 * @Auther: Mcb
 * @Date: 2021/12/12 22:08
 * @Description:
 */
@Configuration
public class JWTConfig {

    public static final String public_sert = "publickey.txt";

    @Bean
    public TokenStore tokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /**
     * 公钥校验密钥加密内容
     * @return
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        Resource resource = new ClassPathResource(public_sert);
        String publicKey = null;
        try {
            publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ////https://www.cnblogs.com/houzheng/p/11380137.html
        //converter.setVerifierKey(publicKey);
        converter.setVerifier(new RsaVerifier(publicKey));
        converter.setSigningKey("mcbmcb");//密钥对密码
        return converter;
    }

}

